package com.kd.juhaowan.ui.adapter;

import androidx.recyclerview.widget.RecyclerView;


import com.kd.juhaowan.ui.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseRecyclerViewAdapter<T extends BaseViewHolder, R> extends RecyclerView.Adapter<T> {

    private final ArrayList<R> data;

    private final int maxItemCount;

    private OnItemClickListener onItemClickListener;

    public BaseRecyclerViewAdapter() {
        this(Integer.MAX_VALUE);
    }

    public BaseRecyclerViewAdapter(int maxItemCount) {
        this.maxItemCount = maxItemCount;
        this.data = new ArrayList<>();
    }

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public final List<R> getAllItems(){
        return new ArrayList<R>(data);
    }

    public boolean isLastItem(int position){
       return position >= getItemCount() -  1;
    }

    public int getMaxItemCount() {
        return maxItemCount;
    }

    public int getSurplusItemCount(){
        return Math.max(maxItemCount - getItemCount(), 0);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemCount() {
        return this.data.size();
    }

    public R getItem(int position) {
        if(this.data.isEmpty()){
            return null;
        }

        if(position < 0 || position >= data.size()){
            return null;
        }

        return this.data.get(position);
    }

    public final void resetAdapterData(List<R> data) {
        this.data.clear();
        if(data != null && !data.isEmpty()){
            this.data.addAll(data);
        }
        checkAdapterDataLimitCount();
        notifyDataSetChanged();
    }

    public final void appendAdapterData(List<R> data) {
        if (data != null && !data.isEmpty()) {
            this.data.addAll(data);
            checkAdapterDataLimitCount();
            notifyDataSetChanged();
        }
    }

    public void updateAdapterData(int position, R newData) {
        if (this.data.isEmpty()) {
            this.data.add(newData);
        } else {
            this.data.remove(position);
            this.data.add(position, newData);
        }
        checkAdapterDataLimitCount();
        notifyItemChanged(position);
    }

    public final void insertAdapterData(int position, List<R> data) {
        if (data != null && !data.isEmpty()) {
            this.data.addAll(position, data);
            checkAdapterDataLimitCount();
            notifyDataSetChanged();
        }
    }

    public final void removeAndAppendAdapterData(int removePosition, R appendData){
        this.data.remove(removePosition);
        this.data.add(appendData);
        notifyDataSetChanged();
    }

    private void checkAdapterDataLimitCount(){
        final int size = this.data.size();
        if(size > maxItemCount){
            for(int index = size - 1; index >= maxItemCount; index--){
                this.data.remove(index);
            }
        }
    }

    public final void notifyItemRemovedViewAndData(int position) {
        this.data.remove(position);
        notifyItemRemoved(position);
    }



}
